using UnityEngine;
using System.Collections;

public class Cannon : MonoBehaviour {
	
	public int moveSpeed = 140;
	public int round = 1;
    public Transform newBullet;
    public Transform bullet;
	public float timeBeforeBullet = 10000;
    public int numberOfBullets = 10;
    public bool isComputer = false;
	Vector3 computerDirection = Vector3.up;
	
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
		ComputerFire();
		ComputerMove();	
	}
	
	
	void ComputerFire()
    {
        timeBeforeBullet *= Time.deltaTime;
        if (numberOfBullets >= 0 && timeBeforeBullet <= 0 && !Bullet.stop)
        {
            newBullet = Instantiate(bullet, transform.position , transform.rotation) as Transform;
			audio.PlayOneShot(audio.clip);
            numberOfBullets--;
            if (numberOfBullets == 0)
            {
                round += 1;
                numberOfBullets = round + 10;
            }
            timeBeforeBullet = 10000;
        }
    }
	
	void ComputerMove()
    {
        Vector3 newPosition = Vector3.zero;

        if (Bullet.stop) newPosition.y = 0;
        
        else if(!Bullet.stop)
        {
        

        newPosition = computerDirection * (moveSpeed * Time.deltaTime);

        newPosition = newPosition + transform.position;

        

        if (newPosition.y > 100)
        {
            newPosition.y = 100;
            computerDirection.y *= -1;
        }
        else if (newPosition.y < -85)
        {
            newPosition.y = -85;
            computerDirection.y *= -1;
        }

        transform.position = newPosition;
        }
    }

}
